﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.google.zxing;
using com.google.zxing.qrcode.encoder;
using com.google.zxing.common;
using System.Drawing.Imaging;

namespace 二维码
{
    class ZxingHelper
    {
        public static Bitmap CreateQRCode(string asset)
        {
            
            ByteMatrix byteMatrix = new MultiFormatWriter().encode(asset, BarcodeFormat.QR_CODE, 200, 200);
            return toBitmap(byteMatrix);
        }

        public static Bitmap toBitmap(ByteMatrix byteMatrix)
        {
            //throw new NotImplementedException();
            int width = byteMatrix.Width;
            int height = byteMatrix.Height;
            Bitmap bmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bmap.SetPixel(x, y, byteMatrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
                }
            }
            return bmap;

        }
        //public String recode(string str)
        //{
        //    string formart = "";
        //    try
        //    {
        //        Boolean ISO = charset.forName()
        //    }
        //    return formart;
        //}
    }
}
